package org.yxing.palpalgps.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.models.RecentChatModel;
import org.yxing.palpalgps.provider.RecentChatsProvider.RecentChats;
import org.yxing.palpalgps.utils.Helper;
import org.yxing.palpalgps.utils.MyBase64;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RecentChatsCursorAdapter extends CursorAdapter {

    private Context mContext;
    private AsyncImageLoader mAsyncImageLoader;
    private SimpleDateFormat dateFormat;
    
    public RecentChatsCursorAdapter(Context context) {
        super(context, null, true);
        mContext = context;
        mAsyncImageLoader = new AsyncImageLoader();
        dateFormat = new SimpleDateFormat("MM-dd HH:mm");
    }

    @Override
    public void bindView(View convertView, Context arg1, Cursor c) {
        RecentChatModel model = new RecentChatModel();
        model.setFuid(c.getString(c.getColumnIndex(RecentChats.FUID)));
        model.setPortrait(c.getString(c.getColumnIndex(RecentChats.PORTRAIT)));
        model.setFname(c.getString(c.getColumnIndex(RecentChats.NAME)));
        model.setNum(c.getInt(c.getColumnIndex(RecentChats.QUANTITY)));
        model.setLastMsg(c.getString(c.getColumnIndex(RecentChats.LAST_MSG)));
        model.setLastTimestamp(c.getLong(c.getColumnIndex(RecentChats.LAST_TIMESTAMP)));
        
        
        //portrait
        ImageView imageViewPortrait = (ImageView) convertView.findViewById(R.id.imageViewPortrait);
        if (!TextUtils.isEmpty(model.getPortrait()))
            mAsyncImageLoader.loadDrawable(Helper.getSmallPictureUrl(model.getPortrait()), imageViewPortrait);
        //set name
        TextView textViewName = (TextView) convertView.findViewById(R.id.textViewName);
        textViewName.setText(model.getFname());
        //set quantity
        TextView textViewQuantity = (TextView) convertView.findViewById(R.id.textViewQuantity);
        textViewQuantity.setText(Integer.toString(model.getNum()));
        //set last message
        TextView textViewLastMsg = (TextView) convertView.findViewById(R.id.textViewLastMsg);
        textViewLastMsg.setText(MyBase64.decode(model.getLastMsg()));
        //set last time
        TextView textViewLastTime = (TextView) convertView.findViewById(R.id.textViewLastTime);
        textViewLastTime.setText(dateFormat.format(new Date(model.getLastTimestamp())));
        
        convertView.setTag(model);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(R.layout.recent_chats_item, null);
    }

}
